package cn.edu.imust.loginregisterdemo.register.view;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.QueryListener;
import cn.edu.imust.loginregisterdemo.R;
import cn.edu.imust.loginregisterdemo.TimeCount;
import cn.edu.imust.loginregisterdemo.base.BaseFragment;
import cn.edu.imust.loginregisterdemo.login.model.User;
import cn.edu.imust.loginregisterdemo.home.view.MainActivity;
import cn.edu.imust.loginregisterdemo.utils.AccountValidatorUtil;
import cn.edu.imust.loginregisterdemo.utils.Utils;

public class SmsRegisterFragment extends BaseFragment {


    @BindView(R.id.rg_phone)
    EditText rgPhone;
    @BindView(R.id.rg_sms_content)
    EditText rgSmsContent;
    @BindView(R.id.rg_get_sms)
    TextView rgGetSms;
    private String currentPhoneNum;
    private String currentSmsCode;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sms_register, container, false);
        Bmob.initialize(getContext(), "33452dfaa0b4f1b9b1a5ea2c1818d5f4");
        ButterKnife.bind(this, view);
        return view;
    }

    @OnClick({R.id.rg_get_sms, R.id.rg_tv_termOfService, R.id.rg_s_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rg_get_sms:
                currentPhoneNum = rgPhone.getText().toString().trim();
                if (TextUtils.isEmpty(currentPhoneNum)) {
                    Utils.showSnackBar(getContext(), view, "手机号不能为空");
                }else if (!AccountValidatorUtil.isMobile(currentPhoneNum)) {
                    Utils.showSnackBar(getContext(), view, "手机号格式不正确");
                }else {
                    if (BmobUser.isLogin()) {
                        User user = BmobUser.getCurrentUser(User.class);
                        Utils.showSnackBar(getContext(), view, "已经登录：" + user.getUsername());
                    } else {

                        /**
                         * TODO template 如果是自定义短信模板，此处替换为你在控制台设置的自定义短信模板名称；如果没有对应的自定义短信模板，则使用默认短信模板，默认模板名称为空字符串""。
                         *
                         * TODO 应用名称以及自定义短信内容，请使用不会被和谐的文字，防止发送短信验证码失败。
                         *
                         */
                        BmobSMS.requestSMSCode(currentPhoneNum, "牧场记录", new QueryListener<Integer>() {
                            @Override
                            public void done(Integer smsId, BmobException e) {
                                if (e == null) {
                                    Utils.showSnackBar(getContext(), view, "发送验证码成功，短信ID：" + smsId);
                                    TimeCount timeCount = new TimeCount(rgGetSms, 60*1000, 1000);
                                    timeCount.start();

                                } else {
                                    Utils.showSnackBar(getContext(), view, "发送验证码失败：" + e.getErrorCode()
                                                    + "-" + e.getMessage());
                                }
                            }
                        });
                    }

                }
                break;
            case R.id.rg_tv_termOfService:
                Utils.showSnackBar(getContext(), view, "服务条款");
                break;
            case R.id.rg_s_login:

                //验证码登录逻辑
                currentPhoneNum = rgPhone.getText().toString().trim();
                currentSmsCode = rgSmsContent.getText().toString().trim();
                if (TextUtils.isEmpty(currentPhoneNum) || TextUtils.isEmpty(currentSmsCode)) {
                    Utils.showSnackBar(getContext(), view, "手机号或验证码不能为空");
                } else if (!AccountValidatorUtil.isMobile(currentPhoneNum)) {
                    Utils.showSnackBar(getContext(), view, "手机号格式不正确");
                }else {
                    Log.d("SmsRegisterFragment", "currentPhoneNum:" + currentPhoneNum + "---" + "currentCode:" + currentSmsCode);
                    //注册并登录
                    ProgressDialog progressDialog = new ProgressDialog(getContext());
                    progressDialog.show();
                    BmobUser.signOrLoginByMobilePhone(currentPhoneNum, currentSmsCode, new LogInListener<BmobUser>() {
                        @Override
                        public void done(BmobUser bmobUser, BmobException e) {
                            if (e == null) {
                                Log.d("SmsRegisterFragment", "mobilePhoneNumber:" + BmobUser.getObjectByKey("mobilePhoneNumber").toString()
                                        + "bmobUser.getUsername():" + bmobUser.getUsername());
                                Utils.showSnackBar(getContext(), view, "短信注册或登录成功：" + bmobUser.getUsername());
                                progressDialog.dismiss();
                                startActivity(new Intent(getContext(), MainActivity.class));
                            } else {
                                progressDialog.show();
                                Log.d("SmsRegisterFragment", "短信注册或登录失败：" + e.getErrorCode()
                                        + "-" +  e.getMessage());
                                Utils.showSnackBar(getContext(), view, "短信注册或登录失败：" + e.getErrorCode()
                                                + "-" + e.getMessage());
                            }
                        }
                    });
                }
                break;
        }
    }




}

